package com.hzpd.hainan.net;


import com.hzpd.hainan.app.App;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class OKHttpHelper {
    private static final int READ_TIMEOUT = 5 * 1000;
    private static final int CONNECT_TIMEOUT = 10 * 1000;

    public static OkHttpClient create() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        File cacheFile = new File(App.getInstance().getCacheDir(), "OkHttpCache");
        Cache cache = new Cache(cacheFile, 10 * 1024 * 1024); //100Mb

        return new OkHttpClient.Builder()
                .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .addInterceptor(interceptor)
                .addNetworkInterceptor(new HttpCacheInterceptor())
                .addNetworkInterceptor(new HeaderParamsInterceptor())
                .cache(cache)
                .build();
    }


}
